package com.cc.mall.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.mall.entity.ProductDetailEntity;
import com.cc.mall.entity.ProductPictureEntity;
import com.cc.mall.service.ProductPictureService;
import com.cc.mall.vo.ProductDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.mall.entity.ProductEntity;
import com.cc.mall.service.ProductService;
import com.cc.common.utils.R;



/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-09 20:57:52
 */
@RestController
@RequestMapping("mall/product")
@Api(tags = "商品")
public class ProductController {

    @Autowired
    private ProductPictureService productPictureService;

    @Autowired
    private ProductService productService;

    /**
     * 上架
     * 把已经上架的商品添加到redis中
     * 修改完之后重新上架，是否要修改redis中的购物车信息
     */
    @ApiOperation("根据商品id进行上架")
    @GetMapping("/up/{id}")
    public R up(@ApiParam("商品id") @PathVariable("id") Long id){
        productService.up(id);
        return R.ok();
    }

    /**
     * 下架
     * 删除redis中的上架商品
     * 修改商品状态
     */
    @GetMapping("/down/{id}")
    public R down(@PathVariable("id") Long id){
        productService.down(id);
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/pageWithCondition")
    public R pageWithCondition(@RequestParam Map<String,Object> params){
        Page<ProductEntity> page =productService.pageWithCondition(params);
        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		/*ProductEntity product = productService.getById(id);
        List<ProductPictureEntity> productPictureEntities = productPictureService.list(new QueryWrapper<ProductPictureEntity>().eq("pdid", id));
        productPictureEntities.stream().s*/
        ProductEntity product = productService.getByIdP(id);
        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ProductEntity product){
		productService.save(product);
		List<ProductPictureEntity> productPictureEntities = new ArrayList<>();
        List<String> bannerPics = product.getBannerPics();
        if(bannerPics !=null && bannerPics.size()>0){
            for (String bannerPic : bannerPics) {
                ProductPictureEntity productPictureEntity = new ProductPictureEntity();
                productPictureEntity.setPdid(product.getId());
                productPictureEntity.setPicture(bannerPic);
                productPictureEntities.add(productPictureEntity);
            }
        }
        if(productPictureEntities !=null && productPictureEntities.size()>0){
            productPictureService.saveBatch(productPictureEntities);
        }
        return R.ok().put("product",product);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody ProductEntity product){
		productService.updateById(product);
        productPictureService.batchDeleteByPdid(product.getId());
        List<ProductPictureEntity> productPictureEntities = new ArrayList<>();
        List<String> bannerPics = product.getBannerPics();
        if(bannerPics !=null && bannerPics.size()>0){
            for (String bannerPic : bannerPics) {
                ProductPictureEntity productPictureEntity = new ProductPictureEntity();
                productPictureEntity.setPdid(product.getId());
                productPictureEntity.setPicture(bannerPic);
                productPictureEntities.add(productPictureEntity);
            }
        }
        if(productPictureEntities !=null && productPictureEntities.size()>0){
            productPictureService.saveBatch(productPictureEntities);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		productService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/getProductDetailListById")
    public R getProductDetailListById(@RequestParam Map<String,Object> params){
        Page<ProductDetailEntity> page = productService.getProductDetailListById(params);
        return R.ok().put("page",page);
    }

    @ApiOperation("通过商品id删除商品相关的数据")
    @GetMapping("/removeById/{id}")
    public R removeById(@ApiParam("商品id") @PathVariable("id") Long id){
        productService.removeByProductId(id);
        productPictureService.batchDeleteByPdid(id);
        return R.ok();
    }

    @ApiOperation("通过商品id查询商品详情")
    @GetMapping("/getProductDetailBySpuId")
    public R getProductDetailBySpuId(@ApiParam("商品id") @RequestParam("spuId") Long spuId){
        List<ProductDetail> productDetail = productService.getProductDetailBySpuId(spuId);
        return R.ok().put("productDetail",productDetail);
    }
}
